package bcared;
import java.sql.*;

//TODO configure user validation for entire site

public class ValidUser {

  private String rolename = "None";
  private int roleid = 0;
  private String password = null;
  private String username = null;
  private int userid = 0;
  private int customerID;
  private PreparedStatement ps = null;
  ResultSet rs = null;
  OraDB myDB = new OraDB();

  public void setPassword(String mypassword)
	throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
	password = mypassword;
  }
  public void setUserName(String myusername) {
		username = myusername;
  }

  public String getPassword()
  throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {

	myDB.createConnection();
	
	String myQuery = "Select u.customerID, u.userid, u.roleid, r.rolename, u.password " +
		" from users u,roles r " +
	" where u.roleid = r.roleid and u.username = ? ";
	
	ps=myDB.conn.prepareStatement(myQuery);
	ps.setString(1,username);
	rs = ps.executeQuery();
	
	while (rs.next()) {
	userid = rs.getInt("userid");
	roleid = rs.getInt("roleid");
	customerID = rs.getInt("customerid");
		rolename = rs.getString("rolename");
	password = rs.getString("password");
	}
return password;
}
  
  public String getRoleName()
      throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
  
  myDB.createConnection();

  String myQuery = "Select u.customerID, u.userid, u.roleid, r.rolename " +  	" from users u,roles r " +
    " where u.roleid = r.roleid and u.username = ? and u.password = ? ";

  ps=myDB.conn.prepareStatement(myQuery);
  ps.setString(1,username);
  ps.setString(2,password);
  rs = ps.executeQuery();

  while (rs.next()) {
	userid = rs.getInt("userid");
	roleid = rs.getInt("roleid");
	customerID = rs.getInt("customerid");
  	rolename = rs.getString("rolename");
  }
   return rolename;
  }
  public int getRoleId() {
	  
	   return roleid;
  }
  
  public int getUserId() {
	   return userid;
 }
  
 public int getCustomerID() {
	 return customerID;
 }
  
 public void closeConnection()
  throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
  	 myDB.closeConnection();
  }
  
}
